import thenFs from 'then-fs'

let hor = 'HOR'
let ver = 'VER'

const map = new Map()

function generateSiteSql(siteId, siteName, projectId) {
  return `
-----------------------------------------------

SELECT
\tCOUNT( DISTINCT a.subject_id ) AS '${siteName}'
FROM
\ttest_complete_info AS a
\tLEFT JOIN user_account AS b ON a.subject_id = b.subject_id
WHERE
\tb.project_id = '${projectId}'
\tAND b.site_id = '${siteId}'

-----------------------------------------------`
}

function generateSiteGenderSql(siteId, siteName, projectId) {
  return `
-----------------------------------------------

SELECT
	b.user_gender AS '性别',
	COUNT( DISTINCT a.subject_id ) AS '${siteName}' 
FROM
	test_complete_info AS a
	LEFT JOIN user_account AS b ON a.subject_id = b.subject_id 
WHERE
	b.project_id = "${projectId}" 
	AND b.site_id = "${siteId}"
GROUP BY b.user_gender

-----------------------------------------------`
}

let sqlStr = ''

// console.log(sqlStr)

map.set('BNU', '北京师范大学')
map.set('PSYCH', '中科院研究所')
map.set('PKU', '北京大学')
map.set('CNU', '首都师范大学')
map.set('BCH', '北京儿童医院')
map.set('PKUH6', '北六院')
map.set('TJNU', '天津师范大学')
map.set('ECNU', '华东师范大学')
map.set('SHANGHAITECH', '上海科技大学')
map.set('SISU', '上海外国语')
map.set('SWU', '西南大学')
map.set('SCNU', '华南师范大学')
map.set('GZFEZX', '广州妇儿中心')
map.set('SIONS', '深圳神科研究院')
map.set('SZKID', '深圳儿童医院')
map.set('NWNU', '西北师范大学')
map.set('GZNU', '贵州师范大学')
map.set('CCNU', '华中师范大学')
map.set('XYEYY', '中南湘雅二院')
map.set('ZZSETYY', '郑州儿童医院')
map.set('CZETYY', '常州儿童医院')
map.set('SEU', '东南大学')
map.set('NENU', '东北师范大学')
map.set('LNNU', '辽宁师范大学')
map.set('SDNU', '山东师范大学')
map.set('UESTC', '电子科技大学')
map.set('SICNU', '四川师范大学')
map.set('SNNU', '陕西师范大学')
map.set('HZNU', '杭州师范大学')
map.set('ZJUCH', '浙大浙儿院')

map.forEach((value, key) => {
  // console.log(`key: ${key}  value: ${value} `)
  sqlStr += generateSiteSql(key, value, hor)
})

thenFs.writeFile(`./files/bhv_site_${hor}.txt`, sqlStr, 'utf-8', (err) => {
  if (!err) {
    console.log('--------------- 文件写入成功 ---------------')
    return
  }
  console.log(err)
})

